package com.hck.domain.doctor.service;

import com.hck.domain.doctor.model.entity.DoctorEntity;
import com.hck.domain.doctor.model.valobj.LoginRequest;
import com.hck.domain.doctor.model.valobj.LoginResult;
import com.hck.domain.doctor.repository.IDoctorRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * DoctorDomainService class description here.
 *
 * @author 黄朝看
 * @date 2025/7/2
 */
@Service
public class DoctorDomainService {

    @Resource
    private IDoctorRepository repository;

    public LoginResult login(LoginRequest loginRequest) {
        //验证参数
        if(!loginRequest.isValid())
            return LoginResult.failure("账号或密码不能为空");

        //根据账号获取医生信息
        DoctorEntity entity = repository.findByLoginName(loginRequest.getLoginName());

        if(entity == null)
            return LoginResult.failure("账号不存在");

        //检查账号状态
        if(!entity.canLogin())
            return LoginResult.failure("账号已禁用");

        if(!entity.validatePassword(loginRequest.getPassword(),entity.getPassword()))
            return LoginResult.failure("密码错误");

        entity.updateLoginInfo();
        repository.update(entity);

        return LoginResult.success(entity.getDoctorId(), entity.getLoginName(), entity.getName());
    }

    public DoctorEntity getDoctorInfo(Long doctorId) {
        return repository.getDoctorInfo(doctorId);
    }

    public List<DoctorEntity> getAllDoctors() {
        return repository.findAll();
    }
}
